package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjCodeScanDetailSet;
import com.zb.mapper.ZjCodeScanDetailSetMapper;
import com.zb.service.ZjCodeScanDetailSetService;
import com.zb.vo.CodeScanDetailSetVo;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 编码扫码详情设置表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-11
 */
@Service
public class ZjCodeScanDetailSetServiceImpl extends ServiceImpl<ZjCodeScanDetailSetMapper, ZjCodeScanDetailSet> implements ZjCodeScanDetailSetService {

    // 初始化 或 恢复初始化编码设置
    @Override
    public void initOrRestoreDefaultCodeScanDetailSet(Long custAccId) {
        ZjCodeScanDetailSet detailSet = new ZjCodeScanDetailSet();
        detailSet.setId(custAccId);
        detailSet.setContent(JSONUtil.toJsonStr(CodeScanDetailSetVo.getDefaultVal()));
        this.saveOrUpdate(detailSet);
    }

    // 根据商家ID获取扫码信息
    @Override
    public List<CodeScanDetailSetVo> getDetail(Long custAccId) {
        ZjCodeScanDetailSet detailSet = this.getById(custAccId);
        if (null != detailSet && StringUtils.isNotBlank(detailSet.getContent()) && JSONUtil.isTypeJSONArray(detailSet.getContent())) {
            return JSONUtil.toList(detailSet.getContent(), CodeScanDetailSetVo.class);
        }
        return CodeScanDetailSetVo.getDefaultVal();
    }

    // 保存编码扫码信息
    @Override
    public void save(Long custAccId, List<CodeScanDetailSetVo> codeScanDetails) {
        Assert.isTrue(CollectionUtil.isNotEmpty(codeScanDetails), "编码扫码信息数据不能为空");

        Assert.isTrue(codeScanDetails.stream().allMatch(code -> Pattern.matches("^(1|2|3|4|5|6|7|8|9)$", code.getType() + "")), "类型参数不合法");
        Assert.isTrue(codeScanDetails.stream().allMatch(code -> Pattern.matches("^(0|1|2|3)$", code.getFieldType() + "")), "自定义字段类型参数不合法");
        Assert.isTrue(codeScanDetails.stream().filter(f -> f.getType() == 1).allMatch(a -> a.getData().stream().allMatch(da -> Pattern.matches("^(1|2|3|4|5)$", da.getDataType() + ""))), "基础选项的数据类型参数不合法");
        Assert.isTrue(codeScanDetails.stream().filter(f -> f.getType() == 3).allMatch(a -> a.getData().stream().allMatch(da -> Pattern.matches("^(1|2|3|4)$", da.getDataType() + ""))), "商品数据类型的参数不合法");
        Assert.isTrue(codeScanDetails.stream().filter(f -> f.getType() == 5).allMatch(a -> a.getData().stream().allMatch(da -> Pattern.matches("^(1|2|3|4|5|6|7|8|9|10|11|12)$", da.getDataType() + ""))), "编码的数据类型参数不合法");
        Assert.isTrue(codeScanDetails.stream().filter(f -> f.getType() == 6).allMatch(a -> a.getData().stream().allMatch(da -> Pattern.matches("^(1|2|3)$", da.getDataType() + ""))), "编码登记的数据类型参数不合法");
        Assert.isTrue(codeScanDetails.stream().filter(f -> f.getType() == 8).allMatch(a -> a.getData().stream().allMatch(da -> Pattern.matches("^(1|2|3|4|5)$", da.getDataType() + ""))), "商品简介与参数说明的数据类型参数不合法");
        if (codeScanDetails.stream().anyMatch(a -> a.getType() == 9)) {
            Assert.isTrue(codeScanDetails.stream().filter(f -> f.getType() == 9).allMatch(a -> a.getData().stream().allMatch(da -> Pattern.matches("^(1|2|3)$", da.getAlign() + ""))), "对齐方式的参数类型不合法");
        }

        ZjCodeScanDetailSet detailSet = new ZjCodeScanDetailSet();
        detailSet.setId(custAccId);
        detailSet.setContent(JSONUtil.toJsonStr(codeScanDetails));
        this.saveOrUpdate(detailSet);
    }
}
